package com.lin.generate.common.utill;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.lin.generate.common.dto.DataSourceBeanDTO;
import com.lin.generate.common.vo.response.UserRespVO;
import com.lin.generate.config.datasource.DataSourceBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * @description TODO
 * @Author: z156940l@outlook.com
 * @className SessionUtil
 * @Date: 2023/6/1 23:27
 */
@Component
public class SessionUtil {
    private static  ThreadLocal<JWT> jwtThreadLocal = new ThreadLocal<>();

    /**
     * 系统登录用户名
     */
    public static final String SESSION_SYSTEM_LOGIN_USER_NAME = "user";

    public static final String SESSION_USER_DB = "dataSource";

    public static final String REDIS_SESSION_HEAD = "generate_session_";

    public static final String SESSION_VERSION = "SESSION_VERSION";

    /** session过期时间 */
    public static long CUSTOMER_SESSION_OVERDUE;
    /** cookie过期时间 */
    public static int CUSTOMER_COOKIE_OVERDUE;

    @Value("${customer.session.overdue}")
    public void setCustomerSessionOverdue(long customerSessionOverdue) {
        CUSTOMER_SESSION_OVERDUE = customerSessionOverdue;
    }

    @Value("${customer.cookie.overdue}")
    public void setCustomerCookieOverdue(int customerCookieOverdue) {
        CUSTOMER_COOKIE_OVERDUE = customerCookieOverdue;
    }

    /**
     * 清空session
     */
    public static final void clearSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(SessionUtil.SESSION_SYSTEM_LOGIN_USER_NAME);
        session.invalidate();
    }


    /**
     * 获取登录用户
     *
     * @return SysUserDO
     */
    public static final UserRespVO getSessionUser() {
        UserRespVO userRsp = null;
        JWT jwt = CookieUtils.getJwt(getRequest(), JsonWebToken.COOKIE_NAME);
        if (jwt != null){
            String userStr = jwt.getHeaderClaim(JsonWebToken.COOKIE_USER_KEY).asString();
            userRsp = JSONObject.parseObject(userStr,UserRespVO.class);
        }
        return userRsp;
    }

    /**
     * 获取登录用户授权版本
     *
     * @return SysUserDO
     */
    public static final String getSessionVersion() {
        String sessionVersion = null;
        JWT jwt = CookieUtils.getJwt(getRequest(), JsonWebToken.COOKIE_NAME);
        if (jwt != null){
            sessionVersion = jwt.getHeaderClaim(SESSION_VERSION).asString();
        }
        return sessionVersion;
    }

    /**
     * 获取请求
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attries = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attries == null) {
            return null;
        }
        return attries.getRequest();
    }

    public static DataSourceBean getDataSourceBean(){
        DataSourceBeanDTO dataSourceBeanDTO = null;
        JWT jwt =jwtThreadLocal.get();
        if (jwt==null){
            HttpServletRequest request = getRequest();
            jwt = CookieUtils.getJwt(request, JsonWebToken.COOKIE_NAME);
        }
        if (jwt != null){
            String userStr = jwt.getHeaderClaim(SessionUtil.SESSION_USER_DB).asString();
            dataSourceBeanDTO = JSONObject.parseObject(userStr,DataSourceBeanDTO.class);
        }
        return dataSourceBeanDTO;
    }
    public static void setJWT(JWT jwt){
        jwtThreadLocal.set(jwt);
    }
}